package com.example.module_service.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.example.lib_common.view.entity.PatrolEntity
import com.example.module_service.databinding.ItemParkBinding
import com.example.module_service.databinding.ItemPatrolBinding

class PatrolAdapter(private val data: MutableList<PatrolEntity>) : RecyclerView.Adapter<PatrolAdapter.PatroItemViewHolder>() {

    private var onItemClickListener: OnItemClickListener? = null

    // 定义点击事件接口
    interface OnItemClickListener {
        fun onItemClick(view: View, patrolEntity: PatrolEntity)
    }

    // 设置点击监听
    fun setOnItemClickListener(listener: OnItemClickListener?) {
        this.onItemClickListener = listener
    }

    inner class PatroItemViewHolder(val itemDataBinging: ItemPatrolBinding) : RecyclerView.ViewHolder(itemDataBinging.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PatroItemViewHolder {
        return PatroItemViewHolder(ItemPatrolBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: PatroItemViewHolder, position: Int) {
    val item = data[position]

    holder.itemDataBinging.run {
        // 巡检类型背景设置
        when (item.patrolType) {
            "保洁巡检" -> {
                tvStatus.setBackgroundResource(com.example.lib_common.R.drawable.shape_solid_circcleorange)
            }
            "安防巡检" -> {
                tvStatus.setBackgroundResource(com.example.lib_common.R.drawable.shape_solid_circleblue)
            }
            else -> {
                tvStatus.setBackgroundResource(com.example.lib_common.R.drawable.shape_solid_circlered)
            }
        }

        // 设置状态文本 - 使用实际的状态值
        if (item.status != null && item.status.isNotEmpty()) {
            status.text = item.status
        } else {
            // 如果状态为空，使用默认逻辑
            status.text = if (item.id % 2 == 0) "点位状态：未启用" else "点位状态：启用"
        }

        // 数据绑定
        entity = item

        // 设置点击事件
        root.setOnClickListener {
            onItemClickListener?.onItemClick(it, item)
        }
    }
}

}
